package controller

import (
	"AccessAdmin/api/backend"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/model/entity"
	"AccessAdmin/internal/service"
	"context"
	"fmt"
	"github.com/gogf/gf/v2/encoding/gjson"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"strings"
)

// Attachment 附件资源
var Attachment = cAttachment{}

type cAttachment struct{}

// ShowIndex 渲染附件资源列表
func (c *cAttachment) ShowIndex(ctx context.Context, req *backend.AttachmentIndexReq) (res *backend.AttachmentIndexRes, err error) {
	service.View().Render(ctx, model.View{
		Title: "附件资源列表",
	})
	return
}

// IndexData 获取附件资源列表
func (c *cAttachment) IndexData(ctx context.Context, req *backend.AttachmentListReq) (res *backend.AttachmentListRes, err error) {
	res = &backend.AttachmentListRes{}
	_data, err := service.Attachment().GetList(ctx, model.AttachmentGetListInput{
		Page:   req.Page,
		Size:   req.Limit,
		Name:   req.Name,
		Status: req.Status,
	})
	if _data != nil && err == nil {
		res.Data = _data.List
		res.Count = _data.Total
	}
	return
}

// ShowCreate 显示附件创建
func (c *cAttachment) ShowCreate(ctx context.Context, req *backend.AttachmentShowCreateReq) (res *backend.AttachmentShowCreateRes, err error) {
	cateTree, err := service.AttachmentCate().GetCateTree(ctx)
	if err != nil {
		return
	}
	jsonData, _ := gjson.EncodeString(cateTree)
	service.View().Render(ctx, model.View{
		Title:   "附件创建",
		MainTpl: "admin/attachment/form.html",
		Data: g.Map{
			"action": "/admin/attachment/create",
			"attachmentModel": &entity.Attachment{
				Status: 1,
			},
			"cateTree": jsonData,
		},
	})
	return
}

// Create 创建资源
func (c *cAttachment) Create(ctx context.Context, req *backend.AttachmentCreateReq) (res *backend.AttachmentCreateRes, err error) {
	var arr []string
	if strings.Index(req.Path, ",") != -1 {
		arr = strings.Split(req.Path, ",")
		req.Name = arr[1]
		req.Path = arr[0]
	}
	base := model.AttachmentCreateUpdateBase{
		CateId: req.CateId,
		Path:   req.Path,
		Name:   req.Name,
		Drive:  "local",
		Status: req.Status,
	}
	_b, err := service.Attachment().SetAttachmentContent(ctx, &base)
	if err != nil {
		return nil, err
	}
	out, err := service.Attachment().Create(ctx, model.AttachmentCreateInput{
		AttachmentCreateUpdateBase: *_b,
	})
	if err != nil {
		return nil, err
	}
	return &backend.AttachmentCreateRes{AttachmentId: out.AttachmentId}, nil
}

// ShowUpdate 显示资源修改
func (c *cAttachment) ShowUpdate(ctx context.Context, req *backend.AttachmentShowUpdateReq) (res *backend.AttachmentShowUpdateRes, err error) {
	cateModel, err := service.Attachment().GetDetail(ctx, model.AttachmentGetDetailInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, gerror.New("资源信息错误!")
	}
	cateTree, err := service.AttachmentCate().GetCateTree(ctx)
	if err != nil {
		return
	}
	jsonData, _ := gjson.EncodeString(cateTree)
	service.View().Render(ctx, model.View{
		Title:   "资源修改",
		MainTpl: "admin/attachment/form.html",
		Data: g.Map{
			"action":          "/admin/attachment/update/" + gconv.String(cateModel.Id),
			"attachmentModel": cateModel,
			"cateTree":        jsonData,
		},
	})
	return
}

// Update 修改资源
func (c *cAttachment) Update(ctx context.Context, req *backend.AttachmentUpdateReq) (res *backend.AttachmentUpdateRes, err error) {
	var arr []string
	if strings.Index(req.Path, ",") != -1 {
		arr = strings.Split(req.Path, ",")
		req.Name = arr[1]
		req.Path = arr[0]
	}
	base := model.AttachmentCreateUpdateBase{
		CateId: req.CateId,
		Path:   req.Path,
		Name:   req.Name,
		Drive:  "local",
		Status: req.Status,
	}
	_b, err := service.Attachment().SetAttachmentContent(ctx, &base)
	if err != nil {
		return nil, err
	}
	err = service.Attachment().Update(ctx, model.AttachmentUpdateInput{
		Id:                         req.Id,
		AttachmentCreateUpdateBase: *_b,
	})
	if err != nil {
		return nil, err
	}
	return &backend.AttachmentUpdateRes{}, nil
}

// ShowView 显示资源修改
func (c *cAttachment) ShowView(ctx context.Context, req *backend.AttachmentShowViewReq) (res *backend.AttachmentShowViewRes, err error) {
	cateModel, err := service.Attachment().GetDetail(ctx, model.AttachmentGetDetailInput{
		Id: req.Id,
	})
	if err != nil {
		fmt.Println(err)
		return nil, gerror.New("资源信息错误!")
	}
	service.View().Render(ctx, model.View{
		Title:   "资源查看",
		MainTpl: "admin/attachment/view.html",
		Data: g.Map{
			"attachmentModel": cateModel,
		},
	})
	return
}

// Delete 删除资源
func (c *cAttachment) Delete(ctx context.Context, req *backend.AttachmentDeleteReq) (res *backend.AttachmentDeleteRes, err error) {
	err = service.Attachment().Delete(ctx, model.AttachmentGetDetailInput{
		Id: req.Id,
	})
	return
}
